import { Component, Injector } from '@angular/core';
import { BasePage } from './../../pages/basePage';
import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { PIC_URL } from "./../../providers/Config";
import { Events } from 'ionic-angular';
declare var $: any;

/**
 * Generated class for the ShouhuoComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'shouhuo',
  templateUrl: 'shouhuo.html'
})
export class ShouhuoComponent extends BasePage {

  showDataNull: boolean;
  service_id: any;
  recommend: any;
  text: string;
  flag: boolean;
  picrooturl = "";//图片服务地址
  userinfo = {}//获取内存中的数据
  orderlist = [];  //获取到的待服务的订单
  pop = 1;    //分页
  bottom1: boolean = false;    //加载全部底部显示器


  constructor(injector: Injector, public userService: UserService, public stc: StorageService,public events: Events) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
    this.getMyOrderListAPI('');
    events.subscribe('user:created',(key)=>{
      if(key == 0){
        console.log("全部有数据更新")
        this.again();
      }
    })
  }

  /**
   * 获取订单数据
   */
  async getMyOrderListAPI(infiniteScroll) {
    let param = { "user_id": this.userinfo['user_id'], "order_type": 1, pop: this.pop };
    let result = await this.userService.getMyOrderListAPI(param);
    console.log(result);
    //成功
    if (result.code == 1) {
      this.pop++;
      this.orderlist = this.orderlist.concat(result.data);
      if (this.orderlist.length == 0){
        this.flag = false;
        this.showDataNull=true;
        this.recommend = result.recommend;
      }else{
        this.flag=true;
        this.showDataNull=false;
      }
      if (infiniteScroll) {
        this.bottom1=false;
        infiniteScroll.complete(); //请求完成数据更新页面
        if (result.data.length < 6) {
          $(".contain1 ion-infinite-scroll-content").css("display","none");
          this.bottom1 = true;
         
        }
      }
    } else {
      this.flag =false;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  }
  /**跳转 到立即购买 */
  toShopInfo(id, shop_id) {
    this.service_id = id;
    this.navCtrl.push("ShopInfoPage", { "id": id, "shop_id": shop_id });
  }

  //无限滚动
  doInfinite(infiniteScroll) {
    this.bottom1 = false;
    this.getMyOrderListAPI(infiniteScroll);
  }

  /**
   * 点击退款跳转申请退款
   * @param val 当前订单信息
   */
  refund(val) {
    this.stc.removeItem("back");
    // this.navCtrl.push("ApplicationPage",{"item": val, "par": 1 })
    // this.navCtrl.push("ApplicationPage")
    let modal = this.modalCtrl.create("ApplicationPage", { "item": val, "par": 1 });
    modal.onDidDismiss(data => {
      if(data == "ok"){
        console.log("成功操作后返回待服务")
        let key=0;
        this.events.publish("user:created",key)
      }
    });
    modal.present();
  }


  /**
   * 点击查看券跳转到服务券页面
   * @param i 点击的订单的订单号
   */
  seeTicket(i) {
    this.stc.removeItem("back");
    console.log(i);
    let params = {
      shop_logo: i.shop_logo,
      shop_name: i.shop_name,
      order_sn: i.order_sn
    }
    //this.navCtrl.push("TicketPage",{tic:params});
    let modal = this.modalCtrl.create("TicketPage", { "tic": params, "par": 1 });
    modal.onDidDismiss(data => {

    });
    modal.present();
  }

  again(){
    this.pop=1;
    this.bottom1 = false;
    this.orderlist=[];
    $(".contain1 ion-infinite-scroll-content").css("display","block");
    this.getMyOrderListAPI('');
  }
}
